﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Carto;

namespace ODM_Code.Forms
{
    public partial class WriteGoogleEarthForm2 : Form
    {
        private IMxDocument doc;
        private IMap map;
        private IActiveView view;
        private ODM_Code.rUtils ru;

        private Dictionary<string, ILayer> checkBoxNameLookup;

        public WriteGoogleEarthForm2()
        {
            InitializeComponent();
        }

        public WriteGoogleEarthForm2(IMxDocument pMxDoc)
        {
            InitializeComponent();

            doc = pMxDoc;
            map = doc.FocusMap;
            view = doc.ActivatedView;
            ru = new ODM_Code.rUtils();

            checkBoxNameLookup = new Dictionary<string, ILayer>();
        }

        private void WriteGoogleEarthForm2_Load(object sender, EventArgs e)
        {
            string strTitle;

            for (int i = 0; i < map.LayerCount; i++)
            {
                if (map.get_Layer(i) == null)
                {
                    break;
                }

                ILayer pLayer = map.get_Layer(i);
                bool isGroupLayerType = pLayer is GroupLayerClass;

                //MessageBox.Show(pLayer.Name + " :: " + isGroupLayerType.ToString());

                if (isGroupLayerType)
                {
                    this.OnLoadHelper(pLayer);
                }
                else
                {
                    strTitle = pLayer.Name;
                    checkBoxNameLookup[strTitle] = pLayer;
                    checkBoxList.Items.Add(strTitle);
                }

            }
        }

        private void exportButton_Click(object sender, EventArgs e)
        {
            view.Refresh();

            // Turn all layers off
            foreach (ILayer layer in checkBoxNameLookup.Values)
            {
                layer.Visible = false;
            }

            doc.UpdateContents();
            view.Refresh();

            folderBrowserDiag.Description = "Select a save location for comparison and other layers.";
            folderBrowserDiag.ShowNewFolderButton = true;
            folderBrowserDiag.ShowDialog();

            ProgressForm pf = new ProgressForm("Exporting to Google Earth", 0, checkBoxList.Items.Count, 1);
            pf.Show();
            pf.Activate();
            pf.BringToFront();

            try
            {
                foreach (object itemChecked in checkBoxList.CheckedItems)
                {
                    ILayer pLayer = checkBoxNameLookup[itemChecked.ToString()];
                    string layerName = pLayer.Name;

                    pf.SetProgressLabel("Exporting " + layerName);
                    ru.ExportToKML(pLayer, folderBrowserDiag.SelectedPath, itemChecked.ToString());

                    pf.StepBar();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message, "GEarth Export");
                MessageBox.Show(ex.GetType().ToString() + " :: " + ex.Message, "GEarth Export");
            }

            pf.Close();
            MessageBox.Show("Export Done.", "KMZ Export");
            view.Refresh();
            this.Close();
        }

        private void OnLoadHelper(ILayer pLayer)
        {
            bool isGroupLayerType = pLayer is GroupLayerClass;
            if (isGroupLayerType)
            {
                ICompositeLayer compLayer = (ICompositeLayer)pLayer;
                if (pLayer.Name.IndexOf("Fire") > 0)
                {
                    string groupName = pLayer.Name.Substring(pLayer.Name.IndexOf("Fire"), 6);
                    
                    for (int i = 0; i < compLayer.Count; i++)
                    {
                        ILayer subLayer = compLayer.get_Layer(i);
                        isGroupLayerType = subLayer is GroupLayerClass;
                        if (isGroupLayerType)
                        {
                            this.OnLoadHelper(subLayer, groupName);
                        }
                    }
                }
            }
        }

        private void OnLoadHelper(ILayer pLayer, string groupName)
        {
            bool isGroupLayerType = pLayer is GroupLayerClass;
            if (isGroupLayerType)
            {
                groupName += " : " + pLayer.Name;
                ICompositeLayer compLayer = (ICompositeLayer)pLayer;
                for (int i = 0; i < compLayer.Count; i++)
                {
                    ILayer subLayer = compLayer.get_Layer(i);
                    this.OnLoadHelper(subLayer, groupName);
                }
            }
            else
            {
                string strTitle = groupName + " " + pLayer.Name;
                checkBoxNameLookup[strTitle] = pLayer;
                checkBoxList.Items.Add(strTitle);
            }

        }
    }
}
